/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.xs_book_agreemnet.service;

import java.util.Date;
import java.util.List;

import com.jeeplus.modules.xs_book.mapper.XsBookMapper;
import com.jeeplus.modules.xs_notice.entity.XsNotice;
import com.jeeplus.modules.xs_notice.mapper.XsNoticeMapper;
import com.jeeplus.modules.xs_notice_user.entity.XsNoticeUser;
import com.jeeplus.modules.xs_notice_user.mapper.XsNoticeUserMapper;
import com.jeeplus.modules.xs_user.mapper.XsUserMapper;
import com.jeeplus.modules.xs_writer.entity.XsWriter;
import com.jeeplus.modules.xs_writer.mapper.XsWriterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.xs_book_agreemnet.entity.XsBookAgreement;
import com.jeeplus.modules.xs_book_agreemnet.mapper.XsBookAgreementMapper;

import javax.annotation.Resource;

/**
 * 书籍协议信息Service
 * @author 天竹子
 * @version 2021-07-15
 */
@Service
@Transactional(readOnly = true)
public class XsBookAgreementService extends CrudService<XsBookAgreementMapper, XsBookAgreement> {
	@Autowired
	XsNoticeMapper xsNoticeMapper;
	@Autowired
	XsBookMapper xsBookMapper;
	@Autowired
	XsNoticeUserMapper xsNoticeUserMapper;
	@Autowired
	XsWriterMapper xsWriterMapper;
	@Autowired
	XsUserMapper xsUserMapper;

	public XsBookAgreement get(String id) {
		return super.get(id);
	}
	
	public List<XsBookAgreement> findList(XsBookAgreement xsBookAgreement) {
		return super.findList(xsBookAgreement);
	}
	
	public Page<XsBookAgreement> findPage(Page<XsBookAgreement> page, XsBookAgreement xsBookAgreement) {
		return super.findPage(page, xsBookAgreement);
	}
	
	@Transactional(readOnly = false)
	public void save(XsBookAgreement xsBookAgreement) {
		XsNotice xsNotice = new XsNotice();
		xsNotice.setType("4");
		xsNotice.setDescribe("您有电子书该签订电子协议啦");
		xsNotice.setTitle("电子书签订协议通知");
		xsNotice.setCreateDate(new Date());
		xsNotice.setContent("<p>您的书籍"+xsBookMapper.get(xsBookAgreement.getBook()).getName()+"可以签订电子协议了，请尽快查看</p>");
		xsNotice.setOtherId(xsBookMapper.get(xsBookAgreement.getBook()).getId());
		xsNoticeMapper.insert(xsNotice);
		XsNoticeUser xsNoticeUser = new XsNoticeUser();
		xsNoticeUser.setIsRead(2);
		xsNoticeUser.setNoticeId(xsNotice.getId());
		xsNoticeUser.setUser(xsUserMapper.get(xsWriterMapper.get(xsBookMapper.get(xsBookAgreement.getBook()).getWriter()).getUser()));
		xsNoticeUserMapper.insert(xsNoticeUser);
		super.save(xsBookAgreement);
	}
	
	@Transactional(readOnly = false)
	public void delete(XsBookAgreement xsBookAgreement) {
		super.delete(xsBookAgreement);
	}
	
}